package xyz.labmem.app.unit

import cn.hutool.core.util.RandomUtil
import javafx.beans.property.SimpleStringProperty
import javafx.event.EventHandler
import javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.control.Label
import javafx.scene.control.Tooltip
import javafx.scene.effect.DropShadow
import javafx.scene.layout.Background
import javafx.scene.layout.HBox
import javafx.scene.paint.Color
import javafx.scene.text.Font
import javafx.scene.text.Text
import xyz.labmem.app.config.AppConfig

class LabelUnit(
    var text: String,
    var size: Double,
    var color: Color = Color.BLACK,
    var backColor: Color = Color.BLACK,
    var oColor: Color = Color.WHITE,
    var shadowColor: Color = Color.WHITE,
    var rotateS: Double = -20.0,
    var rotateE: Double = 20.1,
    var maxStr: Int = -1,
    var clearance: Double = 5.0//间隙
) : HBox() {

    val textProperty = SimpleStringProperty(text)

    private val dropShadow = DropShadow()

    private var ellipsis = false

    private val tip = Tooltip(text)

    init {
        dropShadow.radius = 2.0
        dropShadow.spread = .8
        alignment = Pos.CENTER
        refreshText()
        textProperty.addListener { _, _, t ->
            text = t
            refreshText()
        }
    }

    fun refreshText() {
        children.clear()
        dropShadow.color = shadowColor
        if (text.isNotEmpty()) {
            simpleText(text[0].toString())
        }
        if (text.length > 1) {
            Label(text[1].toString()).apply {
                textFill = oColor
                background = Background.fill(backColor)
                font = Font.loadFont(AppConfig.font, size)
                rotate = RandomUtil.randomDouble(rotateS, rotateE)
                setMargin(this, Insets(.0, clearance, .0, .0))
                this@LabelUnit.children.add(this)
            }
        }
        if (text.length > 2) {
            val l = if (maxStr > 2 && maxStr <= text.length) {
                if (maxStr < text.length) ellipsis = true
                (if (maxStr > 6) maxStr - 1 else maxStr)
            } else (if (text.length > 6) text.length - 1 else text.length)
            for (index in 2..<l) {
                simpleText(text[index].toString())
            }
        }
        if (text.length > 6 && (maxStr == -1 || maxStr > 6)) {
            Label(text[text.length - 1].toString()).apply {
                textFill = oColor
                background = Background.fill(backColor)
                font = Font.loadFont(AppConfig.font, size)
                rotate = RandomUtil.randomDouble(rotateS, rotateE)
                this@LabelUnit.children.add(this)
            }
        }
        if (ellipsis) {
            simpleText("...")
            onMouseEntered = EventHandler {
                tip.show(this, it.screenX, it.screenY + 20)
            }
            onMouseExited = EventHandler {
                tip.hide()
            }
        }
    }

    private fun simpleText(char: String): Text =
        Text(char).apply {
            fill = color
            font = Font.loadFont(AppConfig.font, size)
            rotate = RandomUtil.randomDouble(rotateS, rotateE)
            setMargin(this, Insets(.0, clearance, .0, .0))
            if (shadowColor != Color.TRANSPARENT)
                effect = dropShadow
            this@LabelUnit.children.add(this)
        }


}